//
//  TTMusicRadioViewController.m
//  TTMusic
//
//  Created by chenpengyu on 16/3/30.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "TTMusicRadioViewController.h"
#import "CPYRadioDataDetailModel.h"
#import "TTMusicRadioModel.h"
#import "CPYRadioLeftTableViewCell.h"
#import "CPYRadioRightTableViewCell.h"
#import "CPYSONGSModel.h"
#import "CPYDiscoveryPageSongListModel.h"




@interface TTMusicRadioViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *leftTableView;
@property (nonatomic, strong) UITableView *rightTableView;
@property (nonatomic, assign) NSInteger currentListIndex;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *dataDetailSource;
@property (nonatomic, strong) TTMusicRadioModel *radioDataModel;
@property (nonatomic, strong) CPYSONGSModel *songsModel;
@end

@implementation TTMusicRadioViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:YES];
    
    
}
- (void)creatData {
    [TTMusicAFNetworkTool GETNetWithUrl:@"http://api.fm.ttpod.com/radio" body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
        //        NSLog(@"%@", result);
        for (NSDictionary *dic in result[@"data"]) {
            TTMusicRadioModel *radioDataModel = [[TTMusicRadioModel alloc] initWithDataSource:dic];
            for (CPYRadioDataDetailModel *radioDataDetailModel in radioDataModel.data) {
                [_dataDetailSource addObject:radioDataDetailModel];
            }
            [_dataSource addObject:radioDataModel];
        }
        _currentListIndex=0;
        [_rightTableView reloadData];
        [_leftTableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _currentListIndex=0;
    self.view.backgroundColor = [UIColor whiteColor];
//    self.view.backgroundColor = [UIColor whiteColor];
    [self creatNavButtonItem];

    self.dataSource = [NSMutableArray array];
    self.dataDetailSource = [NSMutableArray array];
    [self createTableView];
    [self creatData];
    // Do any additional setup after loading the view.
}
- (void)creatNavButtonItem {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]  initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
}
- (void)leftBarButtonAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createTableView{
    
    self.leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100 * FitWidth, SHORTHEIGHT) style:(UITableViewStylePlain)];
    _leftTableView.separatorStyle=UITableViewCellSeparatorStyleNone;

    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    _leftTableView.backgroundColor = COLOR(226, 226, 226);
    [self.view addSubview:_leftTableView];
    
    [_leftTableView registerClass:[CPYRadioLeftTableViewCell class] forCellReuseIdentifier:@"CPYRadioLeftTableViewCellIdentifier"];
    _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(100 * FitWidth, 0, 314 * FitWidth, SHORTHEIGHT) style:(UITableViewStylePlain)];
    _rightTableView.delegate = self;
    _rightTableView.dataSource = self;
//    _rightTableView.backgroundColor = [UIColor blueColor];
    [_rightTableView registerClass:[CPYRadioRightTableViewCell class] forCellReuseIdentifier:@"CPYRadioRightTableViewCellIdentifier"];
    _rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_rightTableView];
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_leftTableView == tableView) {
        CPYRadioLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CPYRadioLeftTableViewCellIdentifier"];
        cell.radioModel = _dataSource[indexPath.row];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.isCurrentSelected = _currentListIndex == indexPath.row;
        return cell;
    }else {
        CPYRadioRightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CPYRadioRightTableViewCellIdentifier"];
        cell.dataDetailModel = _dataDetailSource[indexPath.row];
        
        return cell;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_leftTableView == tableView) {
//        _radioDataModel = _dataSource[section];
        return _dataSource.count;
    }else if(_rightTableView == tableView){
        if (_currentListIndex<_dataSource.count) {
            return ((TTMusicRadioModel *)_dataSource[_currentListIndex]).data.count;
        }
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_leftTableView == tableView) {
        return 60 * FitWidth;
    }else {
        return 120 * FitWidth;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_leftTableView == tableView) {
        _currentListIndex=indexPath.row;
        [_dataDetailSource removeAllObjects];
        TTMusicRadioModel *radioDataModel = _dataSource[indexPath.row];
            for (CPYRadioDataDetailModel *radioDataDetailModel in radioDataModel.data) {
                [_dataDetailSource addObject:radioDataDetailModel];
            }
        
        [_leftTableView reloadData];
        [_rightTableView reloadData];
    }else {
        CPYRadioDataDetailModel *radioDataDetailModel = _dataDetailSource[indexPath.row];
        [TTMusicAFNetworkTool GETNetWithUrl:[NSString stringWithFormat:@"http://api.fm.ttpod.com/radio/%@/songs?page_num=1&page_size=20", radioDataDetailModel.tag_id] body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
            self.songsModel = [[CPYSONGSModel alloc] initWithDataSource:result];
            [TTMusicAVPlayer shareTTMusicAVPlayer].local = indexPath.row;
            [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:_songsModel];
        } failure:^(NSError *error) {
            
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
